/**
 * @(#)DownLoadTask.java 1.0 2013/9/4
 * Copyright 2013 PD, Inc. All rights reserved.
 */
package cn.broadin.app.hotel.task;

import cn.broadin.app.hotel.startWebapp.LoadActivity;
import cn.broadin.app.hotel.util.PDUtils;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;



/**
 * DownLoadTask 下载task
 * @author fenghui
 *
 */
@SuppressLint("HandlerLeak")
public class DownLoadTask extends AsyncTask<String[], String, Void>{

	private static final String TAG = "DownLoadTask";		//log tag
	private LoadActivity loadActivity;						//处理完后调用该类中方法
	private ProgressDialog pBar = null;
	private Handler handler = null;
	
	@SuppressLint("HandlerLeak")
	public DownLoadTask(LoadActivity activity) {
		Log.i(TAG, "DownLoadTask start.");
		loadActivity = activity;
		//创建进度条
		pBar = new ProgressDialog(activity);
		pBar.setTitle("更新");
		pBar.setMessage("正在下载更新");
		pBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg){
				switch (msg.what) {
				case -1:		//总长度
					long maxLen = Long.parseLong(msg.obj.toString());
					pBar.setMax((int)maxLen);
					break;
				case 0:			//进度
					pBar.setProgress(msg.arg1);
					break;
				case 2:			//出错
					pBar.dismiss();
					loadActivity.showEeeorMsg("下载出错，稍后再试！");
					break;
				default:
					break;
				}
			}
		};
		pBar.show();
	}

	@Override
	protected Void doInBackground(String[]... params) {
		//开始下载
		PDUtils.getInstance().downAppFile(params[0][0], params[0][1], handler, loadActivity);
		return null;
	}
	
	@Override
	protected void onPostExecute(Void result) {
		pBar.dismiss();
	}
}